package com.meilai.project.service.business.customer.impl;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meilai.project.controller.mini.dto.MiniCustomerNeedSearchParams;
import com.meilai.project.controller.mini.vo.MiniCustomerNeedBuildingAreaVO;
import com.meilai.project.controller.mini.vo.MiniCustomerNeedMTVO;
import com.meilai.project.controller.mini.vo.MiniCustomerNeedVO;
import com.meilai.project.dto.base.SimpleObj;
import com.meilai.project.entity.business.customer.CustomerNeed;
import com.meilai.project.entity.business.customer.CustomerNeedMediaType;
import com.meilai.project.mapper.business.customer.CustomerNeedMapper;
import com.meilai.project.service.business.building.BuildingAreaService;
import com.meilai.project.service.business.customer.CustomerNeedBuildingAreaService;
import com.meilai.project.service.business.customer.CustomerNeedMediaTypeService;
import com.meilai.project.service.business.customer.CustomerNeedService;
import com.meilai.project.service.system.DictionaryService;
import com.meilai.project.vo.business.media.MediaTypeVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 张驰
 * @since 2022-09-02
 */
@Service
public class CustomerNeedServiceImpl extends ServiceImpl<CustomerNeedMapper, CustomerNeed> implements CustomerNeedService {
    @Autowired
    private DictionaryService dictionaryService;
    @Autowired
    private CustomerNeedMediaTypeService customerNeedMediaTypeService;
    @Autowired
    private CustomerNeedBuildingAreaService customerNeedBuildingAreaService;
    @Autowired
    private BuildingAreaService buildingAreaService;

    @Override
    public Page<MiniCustomerNeedVO> selectPageList(MiniCustomerNeedSearchParams params) {
        Page<MiniCustomerNeedVO> page = params.createPage(MiniCustomerNeedVO.class);
        List<MiniCustomerNeedVO> vos = baseMapper.selectPageList(page, params);
        handleVOList(vos);
        page.setRecords(vos);
        return page;
    }

    private void handleVOList(List<MiniCustomerNeedVO> vos){
        if(CollectionUtils.isEmpty(vos)) return;
        List<Long> ids = vos.stream().map(MiniCustomerNeedVO::getId).collect(Collectors.toList());

        List<MiniCustomerNeedBuildingAreaVO> cnbas = customerNeedBuildingAreaService.getVOList(ids);
        Map<Long, List<SimpleObj>> cnbas_map = new HashMap<>();
        if(CollectionUtils.isNotEmpty(cnbas)) {
            cnbas_map = cnbas.stream()
                    .collect(Collectors.groupingBy(MiniCustomerNeedBuildingAreaVO::getCustomer_need_id, Collectors.mapping(d -> {
                        SimpleObj so = new SimpleObj();
                        so.setId(d.getBuilding_area_id());
                        so.setName(d.getName());
                        return so;
                    }, Collectors.toList())));
        }

        List<CustomerNeedMediaType> cnmts = customerNeedMediaTypeService.list(Wrappers.lambdaQuery(CustomerNeedMediaType.class)
                .in(CustomerNeedMediaType::getCustomer_need_id, ids));
        Map<Long, List<MiniCustomerNeedMTVO>> cnmts_map = new HashMap<>();
        if(CollectionUtils.isNotEmpty(cnmts)){
            List<Long> mtIds = cnmts.stream().map(CustomerNeedMediaType::getMedia_type_id).collect(Collectors.toList());
            List<MediaTypeVO> mts = buildingAreaService.getMediaTypes();
            Map<Long, String> mtNamesMap = new HashMap<>();
            if(CollectionUtils.isNotEmpty(mts)) mtNamesMap = mts.stream().collect(Collectors.toMap(MediaTypeVO::getId, MediaTypeVO::getName));
            Map<Long, String> finalMtNamesMap = mtNamesMap;
            cnmts_map = cnmts.stream()
                    .collect(Collectors.groupingBy(CustomerNeedMediaType::getCustomer_need_id, Collectors.mapping(d -> {
                        MiniCustomerNeedMTVO temp = new MiniCustomerNeedMTVO();
                        BeanUtils.copyProperties(d, temp);
                        temp.setMedia_type_name(finalMtNamesMap.getOrDefault(temp.getMedia_type_id(), ""));
                        return temp;
                    }, Collectors.toList())));
        }

        for (MiniCustomerNeedVO vo : vos) {
            vo.setBuildings(cnbas_map.getOrDefault(vo.getId(), new ArrayList<>()));
            vo.setMt_need(cnmts_map.getOrDefault(vo.getId(), new ArrayList<>()));
            vo.setNeed_count_sum(vo.getMt_need().stream().mapToInt(MiniCustomerNeedMTVO::getNeed_count).sum());

        }
    }
}
